<template>
  <div class="hero" >   
    <iframe src="/static/sakura.html" style="width: 100%; height: 100%; z-index: -100;" scrolling="no" frameborder="0"></iframe>
    <div class="form-box">
      <div style="text-align: center;margin-top: 5px">ICT通信双创工作室</div>
      <div class="button-box">
        <div id="btn"></div>
        <button type="button" class="toggle-btn" @click="dologin()">Login</button>
        <button type="button" class="toggle-btn" @click="signup()">Sign Up</button>
      </div>
      <el-form id="login" class="input-group" style="margin-top: 50px" :rules="loginRules" ref="loginForm" :model="loginForm">
        <el-form-item prop="username">
          <input type="text" v-model="loginForm.username" class="input-field" placeholder="Username" required>
        </el-form-item>
        <el-form-item prop="password">
          <input type="password" v-model="loginForm.password" class="input-field" placeholder="Password" required @keyup.enter="handleLogin">
        </el-form-item>
        <a href="https://www.baidu.com" class="forget">Forget?</a>
        <el-button type="warning" @click="handleLogin">Login</el-button>
      </el-form>
      <el-form id="signup" ref="registerForm" class="input-group" :rules="rules" :model="registerForm">
        <div id="box">
          <el-form-item prop="sex" style="height: 20px">
            <span style="color: rgba(235, 248, 241, 0.71);margin-right: 10px;">性别</span>
            <el-radio :label="1" v-model="registerForm.sex" style="color: rgba(235, 248, 241, 0.71);">男</el-radio>
            <el-radio :label="0" v-model="registerForm.sex" style="color: rgba(235, 248, 241, 0.71);margin-left: 0px">女</el-radio>
          </el-form-item>
          <el-form-item prop="name">
            <input type="text" v-model="registerForm.name" class="input-field" placeholder="姓名" required>
          </el-form-item>
          <el-form-item prop="sno">
            <input type="text" v-model="registerForm.sno" class="input-field" placeholder="学号" required>
          </el-form-item>
          <el-form-item prop="dept">
            <input type="text" v-model="registerForm.dept" class="input-field" placeholder="学院名" required>
          </el-form-item>
          <el-form-item prop="major">
            <input type="text" v-model="registerForm.major" class="input-field" placeholder="专业班级" required>
          </el-form-item>
          <el-form-item prop="age">
            <input type="text" v-model="registerForm.age" class="input-field" placeholder="年龄" required>
          </el-form-item>
          <el-form-item prop="email">
            <input type="text" v-model="registerForm.email" class="input-field" placeholder="邮箱" required>
          </el-form-item>
          <el-form-item prop="address">
            <input type="text" v-model="registerForm.address" class="input-field" placeholder="地址" required>
          </el-form-item>
          <el-form-item prop="idcard">
            <input type="text" v-model="registerForm.idcard" class="input-field" placeholder="身份证" required>
          </el-form-item>
          <el-form-item prop="phone">
            <input type="text" v-model="registerForm.phone" class="input-field" placeholder="手机号码" required>
          </el-form-item>
          <el-form-item prop="begintime">
            <el-date-picker
                    class="datepicker"
                    v-model="registerForm.begintime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="入学时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="endtime">
            <el-date-picker
                    class="datepicker"
                    v-model="registerForm.endtime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="毕业时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="username">
            <input type="text" v-model="registerForm.username" class="input-field" placeholder="登录名" required>
          </el-form-item>
          <el-form-item prop="password">
            <input type="password" v-model="registerForm.password" class="input-field" placeholder="登录密码" required>
          </el-form-item>
          <el-form-item prop="comfirmPassword">
            <input type="password" v-model="comfirmPassword" class="input-field" placeholder="确认登录密码" required>
          </el-form-item>
        </div>
        <el-button style="margin-top: 12px" type="warning" @click="register" :disabled="this.flag">Sign Up</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import {validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'


export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      style:"background-image: url('https://s3.ax1x.com/2020/11/15/DFeq78.jpg')",
      comfirmPassword:'',
      rules: {
        name: [
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'change'},
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        sno: [
          {required: true, message: '请输入学号', trigger: 'blur'},
          {min: 11, max: 11, message: '请输入正确的学号', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
        idcard: [
          {required: true, message: '请输入身份证号码', trigger: 'blur'},
          {min:18,max:18,message: '长度必须为18位',trigger: 'blur'}
        ],
        email: [{required: true, message: '请输入电子邮箱', trigger: 'blur'}],
        age: [{required: true, message: '请输入年龄', trigger: 'blur'}],
        username: [{required: true, message: '请输入登录用户名', trigger: 'blur'},{min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'},{min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}],
        phone: [{required: true, message: '请输入手机号码', trigger: 'blur'}],
        address: [{required: true, message: '请输入家庭住址', trigger: 'blur'}],
        begintime: [{required: true, message: '请选择入学日期', trigger: 'change'}],
        endtime: [{required: true, message: '请选择毕业日期', trigger: 'change'}],
        dept: [{required: true, message: '请输入学院名称', trigger: 'change'}],
        major: [{required: true, message: '请输入专业班级', trigger: 'change'}],
      },
      registerForm: {
        name:'',
        username: '',
        password: '',
        email:'',
        sex:'',
        dept:'',
        endtime:'',
        begintime:'',
        major:'',
        address:'',
        age:'',
        idcard:'',
        phone:'',
        sno:'',
        checkState:0,
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message:'请输入正确用户名' },
          {min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur'}
        ],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      flag:false
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    this.getCookie();
    // if (this.loginForm.username === '') {
    //   this.$refs.username.focus()
    // } else if (this.loginForm.password === '') {
    //   this.$refs.password.focus()
    // }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    getCookie(){
      let strs = document.cookie.split("; ")
      for(let s of strs){
        if(s.startsWith("username=")){
          this.loginForm.username=s.substring(9)
        }
        if(s.startsWith("password=")){
          this.loginForm.password=s.substring(9)
        }
      }
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.postKeyValueRequest("/doLogin",this.loginForm).then(resp=>{
            if (resp){
              // alert(JSON.stringify(resp))
              var d = new Date();
              d.setTime(d.getTime() + 5*60*1000);
              document.cookie="username="+this.loginForm.username+"; expires=Mon, 11 Nov 2030 07:34:46 GMT;";
              document.cookie="password="+this.loginForm.password+"; expires=Mon, 11 Nov 2030 07:34:46 GMT;";
              window.sessionStorage.setItem('user',JSON.stringify(resp.obj));
              // console.log("测试："+window.sessionStorage.getItem("user"))
              let path = this.$route.query.redirect;
              this.$router.replace((path=='/'||path==undefined)?'/home':path)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    register(){
      this.$refs['registerForm'].validate(valid => {
        if(valid){
          if(this.comfirmPassword!==this.registerForm.password){
            this.$message.warning("两次输入密码不一致")
          }else{
            this.flag = true
            this.postRequest('/regs',this.registerForm).then(resp=>{
              if(resp){
                this.$notify({
                  title: '成功',
                  message: '恭喜你成功提交注册申请，我们将会在1个工作日内通过邮箱给你答复',
                  type: 'success'
                });
              }
            })
          }
        }else{
          this.$message.warning("请检查输入信息是否有误！")
        }
      })
    },
    signup(){
      var x = document.getElementById("login");
      var y = document.getElementById("signup");
      var z = document.getElementById("btn");
      x.style.left ="115%";
      x.style.right = "10%"
      y.style.left ="15%";
      y.style.right ="10%";
      z.style.left ="110px";
    },
    dologin(){
      var x = document.getElementById("signup");
      var y = document.getElementById("login");
      var z = document.getElementById("btn");
      x.style.left ="115%";
      x.style.right = "10%"
      y.style.left ="15%";
      y.style.right ="10%";
      z.style.left ="0";
    }
  }
}
</script>
<style lang="less">
  .datepicker{
    margin-top: 5px;
    margin-bottom: 5px;
    .el-input__inner{
      background: transparent!important;
      color: #fff;
      font-family: 宋体;
    }
  }
</style>
<style scoped>
  *{
    margin:0;
    padding:0;
  }
  .hero{
    height: 100%;
    width: 100%;
    /*background-image: url("https://s3.ax1x.com/2020/11/15/DFeq78.jpg");*/
    /*background-image: url(tkzc.jpg);*/
    background-position: center;
    background-size: cover;
    position: absolute;
  }
  .form-box{
    width: 400px;
    height: 450px;
    border: 1px solid #000;
    margin: -38% auto;
    position: relative;
    background: #00000080;
    padding: 5px;
    color: #fff;
    overflow: hidden;
  }
  .button-box{
    width: 220px;
    margin: 20px auto;
    position: relative;
    box-shadow: 0 0 20px 3px;
    border-radius: 30px;
  }
  .toggle-btn{
    padding: 10px 30px;
    cursor: pointer;
    background: transparent;
    border: 0;
    outline: none;
    position: relative;
    color: #fff;
    font-size: 15px;
  }
  #btn{
    top: 0;
    left: 0;
    position: absolute;
    width: 50%;
    height: 100%;
    background: linear-gradient(to right,#ff105f,#ffad06);
    border-radius: 30px;
    transition: 0.5s;
  }
  .input-group{
    top: 120px;
    position: absolute;
    width: 280px;
    transition: 0.5s;
  }
  .input-field{
    width: 90%;
    font-size: 16px;
    padding: 10px 0;
    margin: 5px 0;
    border: 0;
    border-bottom: 2px solid #fff;
    outline: none;
    background: transparent;
    color: #fff;
    font-family: 宋体;
  }

  input::placeholder {
    color: rgba(235, 248, 241, 0.71);
  }
  button.el-button , .submit-btn{
    width: 85%;
    padding: 10px 30px;
    cursor: pointer;
    display: block;
    margin: auto;
    background: linear-gradient(to right,#ff105f,#ffad06);
    border: 0;
    outline: none;
    border-radius: 30px;
    font-size: 20px;
    color: wheat;
  }
  .check-box{
    background-position: center;
    margin-top: 20px;
    margin-bottom: 15px;
  }
  .span{
    font-size: 12px;
    bottom: 68px;
    position: absolute;
  }
  #login{
    left: 15%;
    position: absolute;
    right: 10%;
  }
  #signup{
    left: 115%;
    position: absolute;
    right: 10%;
  }
  .forget{
    color: wheat;
    text-decoration: none;
    display:inline-block;
    margin: 10px 30px 20px 110px;
  }
  .code_container{
    margin-top: 10px;
  }
  .btn_send_code{
    font-size: 12px;
    width: 120px;
    color: wheat;
    text-decoration: none;
    display:inline-block;
    margin-left: 10px;
  }
  .input_code{
    font-size: 10.5px;
    width: 140px;
    height: 30px;
  }
  #box{
    height: 230px;
    overflow-x: hidden;
    overflow-y: scroll;
  }
  #box::-webkit-scrollbar {
    width: 2px;
  }
  #box::-webkit-scrollbar-thumb{
    border-radius: 2px;
    height: 50px;
    background: #eee;
  }
  #box::-webkit-scrollbar-track{
    box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    border-radius: 2px;
    background: rgba(0,0,0,0.4);
  }
</style>
